package kr.co.redbean.loader
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import kr.co.redbean.events.ForegroundLoaderEvent;
	import kr.co.redbean.store.IObjectInfo;
	import kr.co.redbean.store.ObjectStore;
	
	public class ForegroundLoader extends EventDispatcher
	{
		private var obj:IObjectInfo;
		
		public function ForegroundLoader(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public function load(menu:IObjectInfo):void {
			obj = menu;
			doLoadObject(obj);
		}
		
		/**
		 * doLoadObject 실행 후 
		 * 1. 이미 로딩이 되어있으면(isLoaded=true) ObjectStore.objectProxyList의 indexof값을 반환:ObjectInfo
		 * 2. 로딩이 안되있으면 (isLoaded=false) ObjectStore.objectProxyList의 해당 ObjectInfo의 isLoaded를 ture로 바꾸고 indexof값을 반환:ObjectInfo
		 * 3. 로딩이 안되있고(isLoaded=false, loaderInfo!=null) ObjectStore.objectProxyList의 loaderInfo에 이벤트리스너를 등록하고 stage로 이벤트 전송. 
		 * 			-> 완료후 indexof값 반환 (hnComplete에서 dispatch할때 포함)
		 */
		
		private function doLoadObject(obj:IObjectInfo):void {
			// setup loader
			
			dispatchEvent(
				new ForegroundLoaderEvent(
					ForegroundLoaderEvent.SETUP, false, false, 0, 0, obj)
			);
			if(obj.isLoaded) { trace(obj.isLoaded); // dispatch COMPLETE when object has been loaded
				dispatchEvent(
					new ForegroundLoaderEvent(
						ForegroundLoaderEvent.COMPLETE, false, false, 0, 0, obj)
				);
			// TODO: 백그라운드 로딩중 포그라운드로 올라가는것 테스트할것
			} else if(obj.onLoading) {  trace(obj.loaderInfo); // dispatch PROGRESS infos when object doing load
				obj.loaderInfo.addEventListener(ProgressEvent.PROGRESS, hnProgress);
				obj.loaderInfo.addEventListener(Event.COMPLETE, hnComplete);
			} else { // when never loaded
				var os:ObjectStore;
				os = ObjectStore.getInstance();

				var rootAD:ApplicationDomain;
				switch(obj.domain) {
					case 'member' : rootAD = os.adStore.memberAD;break;
					case 'menu' : rootAD = os.adStore.menuAD;break;
					case 'popup' : rootAD = os.adStore.popupAD;break;
					case 'assets' : rootAD = os.adStore.assetsAD;break;
				}
				var ad:ApplicationDomain;
				var loader:Loader;
				
				ad = new ApplicationDomain(rootAD);
				
				loader = new Loader();
				loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, hnProgress);
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, hnADLoaded);
				
				obj.onLoading = true;
				obj.loaderInfo = loader;
				
				loader.load(new URLRequest(obj.name+'.swf'), new LoaderContext( false , ad ));

			}
			function hnADLoaded($e:Event):void {
				$e.target.removeEventListener(Event.COMPLETE, hnADLoaded);

				obj.isLoaded = true;
				obj.loaderInfo = null;
				obj.objectProxy = new (ad.getDefinition(obj.name));
				obj.onLoading = false;
				
				dispatchEvent(
					new ForegroundLoaderEvent(
						ForegroundLoaderEvent.COMPLETE, false, false, 0, 0, obj)
				);
			}
			function hnADProgress($e:Event):void {
				
			}
		}
		
		
		// passing event
		private function hnProgress($e:ProgressEvent):void {
			dispatchEvent(
				new ForegroundLoaderEvent(
					ForegroundLoaderEvent.PROGRESS, $e.bubbles, $e.cancelable, $e.bytesLoaded, $e.bytesTotal, obj)
			);
		}
		
		private function hnComplete($e:Event):void {
			obj.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, hnProgress);
			obj.loaderInfo.removeEventListener(Event.COMPLETE, hnComplete);
			dispatchEvent(
				new ForegroundLoaderEvent(
					ForegroundLoaderEvent.COMPLETE, $e.bubbles, $e.cancelable, 0, 0, obj)
			);
		}
	 
	}
}